﻿// -------------------------------------------------------------------------
//    @FileName         :    NFlogUtil.cpp
//    @Author           :    gaoyi
//    @Date             :    24-7-30
//    @Email            :    445267987@qq.com
//    @Module           :    NFlogUtil
//
// -------------------------------------------------------------------------

#include <NFComm/NFCore/NFServerIDUtil.h>
#include <NFComm/NFCore/NFBase64.h>
#include "NFSession.h"
#include "NFPlayer.h"
#include "NFlogUtil.h"

int NFlogUtil::InitTlogTencentHead(NFlogHeader* pstHead)
{
	CHECK_NULL(0, pstHead);
	pstHead->GameSvrId   = NFGlobalSystem::Instance()->GetGlobalPluginManager()->GetBusName();
	pstHead->iZoneAreaID = NFServerIDUtil::GetZoneAreaID(NFGlobalSystem::Instance()->GetGlobalPluginManager()->GetAppID());

	pstHead->dtEventTime = NF_ADJUST_TIMENOW();

	pstHead->vSPTag = ToLog("HE");
	return 0;
}

int NFlogUtil::InitTlogTencentHead(NFlogHeader* pstHead, NFPlayer* pPlayer, NFSession* pSession)
{
	CHECK_NULL(0, pSession);

	InitTlogTencentHead(pstHead);

	if (pSession)
	{
		switch (pSession->GetClientInfo()->plat_type)
		{
			case proto_ff::E_PLAT_ANDROID:
				pstHead->PlatID = 1;
				break; //
			case proto_ff::E_PLAT_IOS:
				pstHead->PlatID = 0;
				break; //
			default:
				pstHead->PlatID = 2;
				break; //unkown
		}

		pstHead->channelID = ToLog(pSession->GetClientInfo()->reg_channel_str.ToString());
		pstHead->vopenid   = ToLog(pSession->GetOpenID());
	}

	if (pPlayer)
	{
		pstHead->vRoleID    = pPlayer->GetRoleID();
		pstHead->vRoleName  = NFBase64::Encode(pPlayer->GetRoleName());
		pstHead->vShowName  = ToLog(pPlayer->GetRoleName());
		/*pstHead->iRoleLevel = pPlayer->GetLevel();
		pstHead->iRoleJob   = pPlayer->GetSkillExt()->GetTransferID();
		pstHead->iRoleCE    = pPlayer->GetCE();
		pstHead->iRolePay   = pPlayer->GetPayExt()->GetTotalPay() * 10;

		CPlayerAccount* pPlayerAccount = CPlayerUtil::GetPlayerAccount(pPlayer);
		if (pPlayerAccount)
		{
			pstHead->iAccountPay = pPlayerAccount->GetTotalPay() * 10;
		}*/
	}

	return 0;
}

int NFlogUtil::InitTlogTencentHead(NFlogHeader* pstHead, const proto_ff::AccountInfo& info)
{
	InitTlogTencentHead(pstHead);

	switch (info.plat_type())
	{
		case proto_ff::E_PLAT_ANDROID:
			pstHead->PlatID = 1;
			break; //
		case proto_ff::E_PLAT_IOS:
			pstHead->PlatID = 0;
			break; //
		default:
			pstHead->PlatID = 2;
			break; //unkown
	}

	pstHead->vopenid = ToLog(info.account_id());

	return 0;
}

std::string NFlogUtil::ToLog(const std::string& str)
{
	std::string log = str;
	for (size_t idx = 0; idx < log.size(); ++idx)
	{
		switch (log[idx])
		{
			case '|':
				log[idx] = '_';
				break;
			case '\b':
			case '\\':
			case '\v':
			case '\r':
			case '\n':
				log[idx] = ' ';
				break;
			default:
				break;
		}
	}

	return log;
}
